package com.burgeon.backend.EnterpriseWeChat.rest.Gtasks;

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class GetDormantVipCount extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetDormantVipCount(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public Integer getData(String empId,String type) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(empId,type), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        Integer count = new JSONArray(str).getJSONObject(0).getInt("count");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        return count;
    }

    private MultiValueMap Data(String empId,String type) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段
        colums.add("'C_VIP_ID;VIPNAME'");//名字


        params.put("columns", colums);
        params.put("table", "DORMANCY_VIP");//表名
        params.put("count", "true");

//        HashMap orderby = new HashMap();
//
//        orderby.put("column", "DAY_BIRTH");
//        orderby.put("asc", "false");
//        ArrayList list = new ArrayList();
//        list.add(orderby);
//
//        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        expr1.put("column","'C_VIP_ID;HR_EMPLOYEE_ID1;ID'");
        String ac = "'"+empId+"'";
        expr1.put("condition",ac);

        HashMap expr2 = new HashMap();
        expr2.put("column","BJ");
        String state= null;
        if(type.equals("1")){
            state="'未回访'";
        }else if(type.equals("2")){
            state="'已回访'";
        }

        expr2.put("condition",state);

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        GetDormantVipCount g = new GetDormantVipCount("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(g.getData("722","2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
